"use strict";
var ServerApi = require('../lib/ServerAPI');
const Controller = require("egg").Controller;
var axios = require('axios');
var utility = require('utility');
var AppKey = '4334a5be55b6d82d147be243294e88f2';
var AppSecret = 'c13ba71393d8';

//创建对象实例
var p = new ServerApi(AppKey, AppSecret);

class TestController extends Controller {

  async getChatToken() {
    const { account } = this.ctx.query;
    const data = await new Promise((resolve, reject) => {
      p.updateUserToken({
        'accid': account,
        'name': '',
        'props': '',
        'icon': '',
        'token': ''
      }, function (err, data) {
        console.log('data', data)
        resolve(data)
      });
    })
    this.ctx.body = {
      token: data.info.token
    }
    // console.log('-----', data)
  }

  async getMembers() {
    // p.updateUserToken({
    //   'accid':'admin',
    //   'name':'',
    //   'props':'',
    //   'icon':'',
    //   'token':''
    // },function(err,data){
    //     // console.log('err',err);
    //     console.log('data',data)
    // });

    this.ctx.body = [
      { id: "1", username: "Jerry", age: 21, gender: "male" },
      { id: "2", username: "Tomy", age: 22, gender: "male" },
      { id: "3", username: "Lily", age: 19, gender: "female" },
      { id: "4", username: "Lucy", age: 20, gender: "female" }
    ];
  }
  async deleteMember() {
    this.ctx.body = {
      code: "1000",
      msg: "success"
    };
  }
  async getlist() {
    this.ctx.body = [
      {
        title: "怎么在一个月内让英语听力有明显的提高？",
        img:
          "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559820680517&di=40398139afed5e198332d1f7a013ce7c&imgtype=0&src=http%3A%2F%2Fs10.sinaimg.cn%2Fmw690%2F002XNwztzy7kaDpRTmxd9%26690",
        answer:
          "一切需要耗费太多意志力支撑的方法都不是好方法。这道题下面已经有很多回答，但一来方法需要,占用的时间太多，耗费大量不必要的意志力，二来步骤不太清晰，容易导致同学们执行的时候有些困惑。"
      },
      {
        title: "哪些资料堪称英专学生的神器？",
        img:
          "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559821157478&di=805e017d89792180f81ba32ad611416d&imgtype=0&src=http%3A%2F%2Fwww.wuhuzhongao.com%2Fimages%2Fobuwgmrxfzxgs4djmmxgg33n%2F20130317%2F6608733_085835479000_2.jpg",
        answer:
          "你可以试试播客、podcast，听外国人的英语，你做影子练习shadowing，他们说什么，你就跟着说什么，或者掐表固定一段时间，例如3Min实时翻译。最后一个比较适合初级写作，开始写作的时候，我们多数还是中式思维,但词汇不够就很难用英语表达出来，这个时候你就可以上这个网站了！"
      },
      {
        title: "超强的学习能力是怎样练就的？",
        img:
          "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1559821501421&di=4f53586ec33774ca0168369a6a1c81e3&imgtype=0&src=http%3A%2F%2Fs15.sinaimg.cn%2Fmw690%2F003hlr4Gzy7n4JN4CpUae%26690",
        answer:
          "下面是我列举的大部分人学习效率不高的常见状态，看看：经常重复阅读，也就是死记硬背,很少回顾当天学习过的知识,碰到不懂的题目，很少努力去思考，经常的状态是，稍微思考一下，脑子反应不出来，就去翻看答案。"
      }
    ];
  }
  async getlistMore() {
    this.ctx.body = [
      {
        title: "任正非为什么说华为迟早得倒下？",
        img:
          "https://www.gucheng.com/hot/UploadFiles_8844/201905/2019052109000907.jpg",
        answer:
          "我几个月之前写过一篇文章。文章中表示华为已死。因为通信设备产业，尤其是基站设备产业早就是没有更多的空间留给华为研发人员（之前写的是通信产业是夕阳产业，的确不太准确）。5G是华为在研发领域最后的狂欢。而华为的新的业务方向，智能手机早就是一片红海。"
      },
      {
        title: "马化腾给张小龙什么样的待遇才能留住他？",
        img:
          "http://zkres1.myzaker.com/201901/5c368c8e6227681850012d9f_960.jpg",
        answer:
          "马化腾可能在其他人的使用上有过失误，或者错过一些人，但在张小龙身上，小马哥一直耐心宽容，坚决支持，展现了一个眼光高远的领袖应该有的气质。最初加入腾讯这个大家庭，是因为腾讯收购了foxmail和业内有口皆碑的张小龙团队。"
      },
      {
        title: "有哪些好的白酒推荐？",
        img:
          "http://zx.images.lcsxjw.com/uploads/20190429/f368e978cfceb616079e6a5b465539a6.jpg",
        answer:
          "10多年的品酒经验，考了个国家级品酒师，尝过大酒小酒千种万种。就个人经验，给大家推荐几款市面上比较好喝的白酒。当然现在我都不喝牌子酒了，一方面因为牌子酒好喝的太贵。不贵的喝的人又太多，品质一般。"
      }
    ];
  }

  async onLogin() {
    const { code } = this.ctx.request.body;
    console.log('====', code)
  }

  async getWeather() {
    const res = await axios.get('http://t.weather.itboy.net/api/weather/city/101210701')
    console.log('=========', res)
    this.ctx.body = {
      provice: res.data.cityInfo.parent,
      city: res.data.cityInfo.city,
      wendu: res.data.data.wendu,
      ganmao: res.data.data.ganmao,
      quality: res.data.data.quality,
      forecast: res.data.data.forecast.map((item) => {
        return {
          week: item.week,
          high: item.high,
          low: item.low,
          type: item.type,
          fx: item.fx
        }
      }).slice(0, 6)
    }
  }

  async fanyi() {

    const { kw } = this.ctx.query;
    const res = await axios.get('https://fanyi-api.baidu.com/api/trans/vip/translate',
      {
        params: {
          q: kw,
          from: 'en',
          to: 'zh',
          appid: '20241119002206584',
          salt: '23332',
          sign: utility.md5('20241119002206584' + kw + '23332' + 'GVTwwWjBv5pI4kvekoZd')
        }
      }
    )
    console.log(kw)
    console.log(res.data)
    this.ctx.body = {
      data: res.data
    }
  }
  async fanyi1() {


    const { kw } = this.ctx.query;

    // const headers = {
    //   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    //   'Accept-Encoding': 'gzip, deflate, br',
    //   'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    //   'Cache-Control': 'no-cache',
    //   'Connection': 'keep-alive',
    //   'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
    //   'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"'
    // }


    // superagent
    //   .get(`https://fanyi.baidu.com/sug?kw=${kw}`)
    //   .set('Accept', headers['Accept'])
    //   .set('Accept-Encoding', headers['Accept-Encoding'])
    //   .set('Accept-Language', headers['Accept-Language'])
    //   .set('Cache-Control', headers['Cache-Control'])
    //   .set('Connection', headers['Connection'])
    //   .set('User-Agent', headers['User-Agent'])
    //   .set('sec-ch-ua', headers['sec-ch-ua'])
    //   .end((err, res) => {
    //     if (err) {
    //       console.log(`访问失败 - ${err}`)
    //     } else {
    //       console.log(kw)
    //       console.log(res)
    //     }
    //   });

    this.ctx.body = {
      data: res.data.data
    }
  }

  async robot() {

    const { kw } = this.ctx.query;
    const res = await axios.post('https://spark-api-open.xf-yun.com/v1/chat/completions', {
      model: 'lite',
      "messages": [
        {
          "role": "user",
          "content": kw
        }
      ],
      "stream": false
    }, {
      headers: {
        "Authorization": "Bearer EqUmcARNdMeYAyknGUDe:tUSDqwOBAOiFSkdUCAjb"
      }
    })

    console.log(res.data)
    this.ctx.body = {
      data: res.data
    }
  }
}

module.exports = TestController;
